package com.yzd.learnjava.concurrency;

import java.util.Random;
import java.util.concurrent.TimeUnit;

import com.yzd.learnjava.util.Print;

public class Philosopher implements Runnable{
	private Chopstick left;
	private Chopstick right;
	private final int id ;
	private final int ponderFactor;
	private Random random = new Random(47);
	private void pause() throws InterruptedException {
		if(ponderFactor == 0) {
			return;
		}
		TimeUnit.MILLISECONDS.sleep(random.nextInt(ponderFactor * 250));
		
	}
	public Philosopher(Chopstick left, Chopstick right, int ident, int ponder) {
		this.left = left;
		this.right = right;
		id = ident;
		ponderFactor = ponder;
	}
	
	public void run() {
		try {
			while(!Thread.interrupted()) {
				Print.print(this + " thinking");
				pause();
				
				Print.print(this + " grabbing right");
				right.take();
				
				Print.print(this + " grabbing left");
				left.take();
				
				Print.print(this + " eating");
				pause();
				right.drop();
				left.drop();
			}
		} catch (InterruptedException e) {
			Print.print(this + " exiting via Interrupted");
		}
	}
	
	public String toString() {
		return "Philosopher " + id;
	}
}
